package com.mililoan.persist.system.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.mililoan.common.dao.AbstractBaseDao;
import com.mililoan.common.utils.StackTraceUtil;
import com.mililoan.model.system.Menu;
import com.mililoan.persist.system.MenuDao;

@Repository("menuDao")
public class MenuDaoImpl extends AbstractBaseDao<Menu> implements MenuDao {
	private static final Logger LOGGER = LoggerFactory.getLogger(MenuDaoImpl.class);

	public Map<Integer, Menu> getMenu() {
		JdbcTemplate template = this.getJdbcTemplate();
		final Map<Integer, Menu> menuMap = new LinkedHashMap<Integer, Menu>();
		String sql = "SELECT * FROM SYS_MENU order by MENU_ID";
		try {
			template.query(sql.toString(), new RowCallbackHandler() {
				public void processRow(ResultSet rs) throws SQLException {
					Menu menu = new Menu();
					menu.setMenuId(rs.getInt("MENU_ID"));
					menu.setMenuName(rs.getString("MENU_NAME"));
					menu.setMenuIcon(rs.getString("MENU_ICON"));
					menu.setParentMenu(rs.getInt("PARENT_MENU"));
					menu.setMenuUrl(rs.getString("MENU_URL"));
					menu.setMenuLevel(rs.getInt("MENU_LEVEL"));
					menuMap.put(rs.getInt("MENU_ID"), menu);
				}
			});
			return menuMap;
		} catch (Exception e) {
			LOGGER.error("查询所有菜单getMenu异常", StackTraceUtil.getStackTrace(e));
			return null;
		}
	}

	public List<Menu> getAllMenu() {
		JdbcTemplate template = this.getJdbcTemplate();
		final List<Menu> menuList = new ArrayList<Menu>();
		String sql = "SELECT * FROM SYS_MENU order by MENU_ID";
		try {
			template.query(sql.toString(), new RowCallbackHandler() {
				public void processRow(ResultSet rs) throws SQLException {
					Menu menu = new Menu();
					menu.setMenuId(rs.getInt("MENU_ID"));
					menu.setMenuName(rs.getString("MENU_NAME"));
					menu.setMenuIcon(rs.getString("MENU_ICON"));
					menu.setParentMenu(rs.getInt("PARENT_MENU"));
					menu.setMenuUrl(rs.getString("MENU_URL"));
					menu.setMenuLevel(rs.getInt("MENU_LEVEL"));
					menuList.add(menu);
				}
			});
			return menuList;
		} catch (Exception e) {
			LOGGER.error("查询所有菜单getAllMenu异常", StackTraceUtil.getStackTrace(e));
			return null;
		}
	}

}
